package band.wukong.practice.java.extension.topic.script;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineFactory;
import javax.script.ScriptEngineManager;
import java.util.*;

/**
 * @author wukong(wukonggg@139.com)
 */
public class ScriptEngineManagerSample {

    private ScriptEngineManager manager;

    public ScriptEngineManagerSample() {
        manager = new ScriptEngineManager();
    }

    public Map<String, String> factoryInfo() {
        Map<String, String> ret = new LinkedHashMap<>();
        List<ScriptEngineFactory> factories = manager.getEngineFactories();
        for (ScriptEngineFactory fact : factories) {
            ret.put("Engine Name", fact.getEngineName());
            ret.put("Engine Version", fact.getEngineVersion());
            ret.put("Language Name", fact.getLanguageName());
            ret.put("Language Version", fact.getLanguageVersion());
            ret.put("Extensions", Arrays.toString(fact.getExtensions().toArray()));
            ret.put("MimeTypes", Arrays.toString(fact.getMimeTypes().toArray()));
            ret.put("Names", Arrays.toString(fact.getNames().toArray()));
        }
        return ret;
    }

    public List<ScriptEngine> getEngineByXxx() {
        List<ScriptEngine> ret = new ArrayList<>();

        ScriptEngine e1 = manager.getEngineByName("javascript");
        ScriptEngine e2 = manager.getEngineByExtension("js");
        ScriptEngine e3 = manager.getEngineByMimeType("text/javascript");

        ret.add(e1);
        ret.add(e2);
        ret.add(e3);

        return ret;
    }
}
